<?php

namespace Backoffice\Controller;

use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Backoffice\Model\Entity\Annonce;

class AnnonceController extends AbstractActionController
{
    /**
    * Entity manager instance
    *
    * @var Doctrine\ORM\EntityManager
    */
    protected $em;

    /**
    * Returns an instance of the Doctrine entity manager loaded from the service
    * locator
    *
    * @return Doctrine\ORM\EntityManager
    */
    
    public function getEntityManager()
    {
        if (null === $this->em) {
            $this->em = $this->getServiceLocator()
                ->get('doctrine.entitymanager.orm_default');
        }
        return $this->em;
    }
    
    public function indexAction()
    {
        return new ViewModel(
            array(
                'annonces' => $this->getEntityManager()->getRepository('Backoffice\Model\Entity\Annonce')->findAll()
            )
        );
    }
    
    public function addAction()
    {
        $sm = $this->getServiceLocator();
        $session = $sm->get('zfcuser_auth_service');
        $identity = $session->getIdentity();
        $user_id = $identity->getId();

        $form = new \Backoffice\Form\AnnonceForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $annonce = new Annonce();
            $utilisateur = $this->getEntityManager()->getRepository('Backoffice\Model\Entity\Utilisateur')->find($user_id);
            
            $annonce->setUtilisateur($utilisateur);
            $form->setInputFilter($annonce->getInputFilter());
            $form->setData($request->getPost());
            
            
            if ($form->isValid()) {
                $annonce->populate($form->getData());
                $this->getEntityManager()->persist($annonce);
                $this->getEntityManager()->flush();

                // Redirect to list of annonces
                return $this->redirect()->toRoute('annonce');
            }
        }
        return array('form' => $form);
    }

    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('annonce', array(
                'action' => 'add'
            ));
        }
        $annonce = $this->getEntityManager()->find('Backoffice\Model\Entity\Annonce', $id);
        if (is_null($annonce)) {
            return $this->redirect()->toRoute('annonce');
        }
        
        $form  = new \Backoffice\Form\AnnonceForm();
        
        $form->bind($annonce);
        $form->get('submit')->setAttribute('value', 'Edit');

        $request = $this->getRequest();
        
        if ($request->isPost()) {
            
            $form->setInputFilter($annonce->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $form->bindValues();
                $this->getEntityManager()->flush();

                // Redirect to list of annonces
                return $this->redirect()->toRoute('annonce');
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    public function deleteAction()
    {
        $error = null;
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('annonce');
        }
        
        $em = $this->getEntityManager();
        $annonce = $em->find('Backoffice\Model\Entity\Annonce', $id);
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');
            if ($del == 'Yes') {
                if ($annonce) {
                    // Supprimer l'annonce
                    $em->remove($annonce);
                    $em->flush();
                }
            }

            // Redirect to list of annonces
            return $this->redirect()->toRoute('annonce');
        }
        
        if (is_null($annonce)) {
            $error = "L'annonce numero $id n'existe pas !";
        }

        return array(
            'id'    => $id,
            'annonce' => $annonce,
            'error' => $error,
        );
    }


}

